using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
	public class ServiceData
	{
		#region Fields
		protected static SqlConnection dbConn = new SqlConnection(Config.Global.Settings.ConnectionString);
		#endregion

		#region Get Object Method
		private static object getNullObject(object obj)
		{
			return obj == null ? DBNull.Value : obj;
		}
		private static object getNullValue(object obj)
		{
			return obj == DBNull.Value ? null : obj;
		}
		#endregion

		#region Constructors
		public ServiceData() {}
		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Service table.
		/// </summary>
		/// <param name="serviceInfo"></param>
		/// <returns>ID of Saved record</returns>
		public static int? Insert(ServiceInfo serviceInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspServiceInsert]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(serviceInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@ServiceName", getNullObject(serviceInfo.ServiceName));
			dbCmd.Parameters.AddWithValue("@ServiceImage", getNullObject(serviceInfo.ServiceImage));
			dbCmd.Parameters.AddWithValue("@ServiceDescription", getNullObject(serviceInfo.ServiceDescription));
			dbCmd.Parameters.AddWithValue("@ServiceContent", getNullObject(serviceInfo.ServiceContent));
			dbCmd.Parameters.AddWithValue("@ServiceURL", getNullObject(serviceInfo.ServiceURL));
			dbCmd.Parameters.AddWithValue("@ServiceStatus", getNullObject(serviceInfo.ServiceStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value);
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Updates a record in the Service table.
		/// </summary>
		/// <param name="serviceInfo"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool Update(ServiceInfo serviceInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspServiceUpdate]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@ServiceID", getNullObject(serviceInfo.ServiceID));
			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(serviceInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@ServiceName", getNullObject(serviceInfo.ServiceName));
			dbCmd.Parameters.AddWithValue("@ServiceImage", getNullObject(serviceInfo.ServiceImage));
			dbCmd.Parameters.AddWithValue("@ServiceDescription", getNullObject(serviceInfo.ServiceDescription));
			dbCmd.Parameters.AddWithValue("@ServiceContent", getNullObject(serviceInfo.ServiceContent));
			dbCmd.Parameters.AddWithValue("@ServiceURL", getNullObject(serviceInfo.ServiceURL));
			dbCmd.Parameters.AddWithValue("@ServiceStatus", getNullObject(serviceInfo.ServiceStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the Service table by its primary key.
		/// </summary>
		/// <param name="serviceID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteByServiceID(int serviceID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspServiceDeleteByServiceID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@ServiceID", serviceID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the Service table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspServiceDeleteAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects a single record from the Service table.
		/// </summary>
		/// <param name="serviceID"></param>
		/// <returns>ServiceInfo: A record from the Service table by its primary key.</returns>
		public static ServiceInfo SelectByServiceID(int serviceID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspServiceSelectByServiceID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			ServiceInfo serviceInfo =  new ServiceInfo();

			dbCmd.Parameters.AddWithValue("@ServiceID", serviceID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.Read())
				{
					serviceInfo.ServiceID = System.Convert.ToInt32(getNullValue(dataReader["ServiceID"]));
					serviceInfo.SiteID = System.Convert.ToInt32(getNullValue(dataReader["SiteID"]));
					serviceInfo.ServiceName = System.Convert.ToString(getNullValue(dataReader["ServiceName"]));
					serviceInfo.ServiceImage = System.Convert.ToString(getNullValue(dataReader["ServiceImage"]));
					serviceInfo.ServiceDescription = System.Convert.ToString(getNullValue(dataReader["ServiceDescription"]));
					serviceInfo.ServiceContent = System.Convert.ToString(getNullValue(dataReader["ServiceContent"]));
					serviceInfo.ServiceURL = System.Convert.ToString(getNullValue(dataReader["ServiceURL"]));
					serviceInfo.ServiceStatus = System.Convert.ToBoolean(getNullValue(dataReader["ServiceStatus"]));
				}

				dataReader.Close();
				return serviceInfo;
			}
			catch (Exception)
			{
				return new ServiceInfo();
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the Service table.
		/// </summary>
		/// <return>DataTable</return>
		public static DataTable SelectAll()
		{
			SqlCommand dbCmd = new SqlCommand("[uspServiceSelectAll]",dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbService =  new DataTable();

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbService.Load(dataReader);
				}

				dataReader.Close();
				return dtbService;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the Service table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <return>All records from the Service table by a foreign key.</return>
		public static DataTable SelectAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspServiceSelectAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbService =  new DataTable();

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbService.Load(dataReader);
				}

				dataReader.Close();
				return dtbService;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		#endregion
	}
}
